﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using papercut2.Data.Global;
using System.Drawing;
using System.Collections;
using papercut2.Data.ImageProcess;

namespace papercut2.Data.ImageProcess
{
    class GetTrace
    {
        private static int[,] direction = new int[8, 2]{{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
        private static Bitmap bm = null;
        //边界跟踪后得到的多条轮廓的起点链表
        private static LinkedList<bpoint> PstartList = null;

        public static LinkedList<bpoint> pStartList
        {
            get
            {
                return PstartList;
            }
            set
            {
                PstartList = value;
            }
        }

        public GetTrace()//边界跟踪。。。。。。。。。。
        {
        
        }

        public static void Initial()
        {
            bm = GetBorder.Pic;
        }

        public static LinkedList<bpoint> getTotalTrace()
        {
            Initial();
            IDictionaryEnumerator enumerator = GetBorder.Bp.GetEnumerator(); 
            Color black = Color.FromArgb(0, 0, 0);
            while (enumerator.MoveNext())
            {
                bpoint p = (bpoint)enumerator.Value;
                if (p.Flag == 0)
                {
                    bpoint temp = getSubTrace(p);
                    if (PstartList.Count == 0)
                    {
                        PstartList.AddFirst(temp);
                    }
                    else
                    {
                        PstartList.AddLast(temp);
                    }
                    bpoint pp = p;
                    GetBorder.remove(p);
                    pp.Flag = 1;
                    GetBorder.put(pp);
                }

            }

            return PstartList;
        }

 

        
        //获取单条曲线的轮廓
        public static bpoint getSubTrace(bpoint pstart)
        {
            int i = 0;//i for direction.......
            bpoint pcur = pstart;//pcur是当前边界点
            bpoint pnext = null;
            while (true)
            {
                pnext = getNext(pcur.X, pcur.Y, i);//获得当前边界点的下一个检测点
                if (isBorder(pnext.X, pnext.Y))
                {
                    if (isSamePoint(pnext, pstart))//返回开始点，结束
                    {
                        break;
                    }
                    else
                    {
                        //.....回退两次，90度
                        i--;
                        if (i == -1)
                            i = 7;
                        i--;
                        if (i == -1)
                            i = 7;
                    }
                    pcur.Next = pnext;
                    pcur = pnext;

                }
                else
                {
                    i++;
                    if (i == 8)
                        i = 0;
                }
            }

            pcur.Next = pnext;
            pcur = pnext;
            return pstart;//返回链表头部。。。。。
        }


        private static Boolean isBorder(double x, double y)
        {
            Color black = Color.FromArgb(0, 0, 0);
            if (bm.GetPixel((int)x, (int)y) == black)
                return true;
            else
                return false;
        }
        private static bpoint getNext(double x, double y, int k)
        {
            bpoint cur = new bpoint(x + direction[k, 0], y + direction[k, 1], 0, 0, null);

            return cur;
        }
        private static Boolean isSamePoint(bpoint a, bpoint b)
        {
            if ((a.X == b.X) && (a.Y == b.Y))
                return true;
            else
                return false;
        }
    }
}
